<?php

    #-------------------------------------------------------------#
	# This file is part of the Tekuna MVC Microframework for PHP. #
	# (c) 2009 Niels Jäckel <niels.jaeckel@tekuna.org>            #
	# Please visit http://tekuna.org/ for more information.       #
	#                                                             #
	# This software is licensed under the MIT License. Please     #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#


	Tekuna :: load('org.tekuna.core.config.ApplicationConfiguration');



	class ApplicationConfigTest extends AbstractTekunaTestCase {

		public function testComponentRoute() {

			// get route instance
			$objRoute = new ApplicationComponentRoute();

			// test default values
			$this -> assertNull($objRoute -> getPattern());
			$this -> assertNull($objRoute -> getFragment());
			$this -> assertNull($objRoute -> getController());
			$this -> assertNull($objRoute -> getStatus());

			$this -> assertFalse($objRoute -> hasFragment());
			$this -> assertFalse($objRoute -> hasController());
			$this -> assertFalse($objRoute -> hasStatus());
			$this -> assertFalse($objRoute -> isValid());


			// test getters and setters and valid status
			$sPattern = '*.html';
			$objRoute -> setPattern($sPattern);
			$this -> assertIdentical($objRoute -> getPattern(), $sPattern);
			$this -> assertFalse($objRoute -> isValid());

			$sFragment = 'myFragment';
			$this -> assertFalse($objRoute -> hasFragment());
			$objRoute -> setFragment($sFragment);
			$this -> assertIdentical($objRoute -> getFragment(), $sFragment);
			$this -> assertTrue($objRoute -> hasFragment());
			$this -> assertTrue($objRoute -> isValid());

			$iStatus = 201;
			$this -> assertFalse($objRoute -> hasStatus());
			$objRoute -> setStatus($iStatus);
			$this -> assertIdentical($objRoute -> getStatus(), $iStatus);
			$this -> assertTrue($objRoute -> hasStatus());
			$this -> assertTrue($objRoute -> isValid());

			$sController = 'MyTestController';
			$this -> assertFalse($objRoute -> hasController());
			$objRoute -> setController($sController);
			$this -> assertIdentical($objRoute -> getController(), $sController);
			$this -> assertTrue($objRoute -> hasController());
			$this -> assertFalse($objRoute -> isValid());
		}


		public function testComponent() {

			// get component instance
			$objComponent = new ApplicationComponent();

			// test default values
			$this -> assertNull($objComponent -> getSlot());
			$this -> assertNull($objComponent -> getFragment());
			$this -> assertNull($objComponent -> getController());
			$this -> assertNull($objComponent -> getStatus());
			$this -> assertIdentical($objComponent -> getAllRoutes(), array());

			$this -> assertFalse($objComponent -> hasFragment());
			$this -> assertFalse($objComponent -> hasController());
			$this -> assertFalse($objComponent -> hasRoutes());
			$this -> assertFalse($objComponent -> hasStatus());
			$this -> assertFalse($objComponent -> isValid());


			// test getters and setters and valid status
			$sSlot = 'mySlot';
			$objComponent -> setSlot($sSlot);
			$this -> assertIdentical($objComponent -> getSlot(), $sSlot);
			$this -> assertFalse($objComponent -> isValid());

			$sFragment = 'fragment.htmlf';
			$this -> assertFalse($objComponent -> hasFragment());
			$objComponent -> setFragment($sFragment);
			$this -> assertTrue($objComponent -> hasFragment());
			$this -> assertIdentical($objComponent -> getFragment(), $sFragment);
			$this -> assertTrue($objComponent -> isValid());

			$sController = 'MyTestController';
			$this -> assertFalse($objComponent -> hasController());
			$objComponent -> setController($sController);
			$this -> assertTrue($objComponent -> hasController());
			$this -> assertIdentical($objComponent -> getController(), $sController);
			$this -> assertFalse($objComponent -> isValid());

			$iStatus = 301;
			$this -> assertFalse($objComponent -> hasStatus());
			$objComponent -> setStatus($iStatus);
			$this -> assertTrue($objComponent -> hasStatus());
			$this -> assertIdentical($objComponent -> getStatus(), $iStatus);
			$this -> assertFalse($objComponent -> isValid());

			$objRoute = new ApplicationComponentRoute();
			$objRoute -> setPattern('*.html');
			$objRoute -> setController('AnotherTestController');
			$this -> assertFalse($objComponent -> hasRoutes());
			$objComponent -> addRoute($objRoute);
			$this -> assertTrue($objComponent -> hasRoutes());
			$this -> assertIdentical($objComponent -> getAllRoutes(), array($objRoute));
			$this -> assertFalse($objComponent -> isValid());
		}


		public function testModule() {

			// get component instance
			$objModule = new ApplicationModule();

			// test default values
			$this -> assertIdentical($objModule -> getBaseURL(), '/');
			$this -> assertIdentical($objModule -> getLayout(), '');
			$this -> assertNull($objModule -> getStatus());
			$this -> assertIdentical($objModule -> getAllComponents(), array());
			$this -> assertFalse($objModule -> hasStatus());
			$this -> assertFalse($objModule -> isValid());


			// test getters and setters and valid status
			$sBaseURL = '/myModule/';
			$objModule -> setBaseURL($sBaseURL);
			$this -> assertIdentical($objModule -> getBaseURL(), $sBaseURL);
			$this -> assertFalse($objModule -> isValid());

			$sLayout = 'baseLayout.tpl';
			$objModule -> setLayout($sLayout);
			$this -> assertIdentical($objModule -> getLayout(), $sLayout);
			$this -> assertTrue($objModule -> isValid());

			$iStatus = 404;
			$this -> assertFalse($objModule -> hasStatus());
			$objModule -> setStatus($iStatus);
			$this -> assertIdentical($objModule -> getStatus(), $iStatus);
			$this -> assertTrue($objModule -> hasStatus());
			$this -> assertTrue($objModule -> isValid());

			$objComponent = new ApplicationComponent();
			$objComponent -> setSlot('mySlot');
			$objComponent -> setFragment('myFragment.htmlf');
			$this -> assertIdentical($objModule -> getAllComponents(), array());
			$objModule -> addComponent($objComponent);
			$this -> assertIdentical($objModule -> getAllComponents(), array($objComponent));
			$this -> assertTrue($objModule -> isValid());

			$objModule -> setLayout(' ');
			$this -> assertFalse($objModule -> isValid());
		}


		public function testConfiguration() {

			// get configuration instance
			$objConfiguration = new ApplicationConfiguration();

			// test default values
			$this -> assertIdentical($objConfiguration -> getLanguages(), array());
			$this -> assertIdentical($objConfiguration -> getAllModules(), array());
			$this -> assertFalse($objConfiguration -> isValid());


			// test getters and setters and valid status
			$arrLanguages = array('en', 'de', 'fr');
			$objConfiguration -> setLanguages($arrLanguages);
			$this -> assertIdentical($objConfiguration -> getLanguages(), $arrLanguages);
			$this -> assertFalse($objConfiguration -> isValid());

			$objModule = new ApplicationModule();
			$objModule -> setLayout('myLayout.tpl');
			$this -> assertIdentical($objConfiguration -> getAllModules(), array());
			$objConfiguration -> addModule($objModule);
			$this -> assertIdentical($objConfiguration -> getAllModules(), array($objModule));
			$this -> assertTrue($objConfiguration -> isValid());
		}


		public function testValidationExceptions() {

			// create configuration objects
			$objRoute = new ApplicationComponentRoute();
			$objComponent = new ApplicationComponent();
			$objModule = new ApplicationModule();
			$objConfiguration = new ApplicationConfiguration();

			// ensure invalidity
			$this -> assertFalse($objRoute -> isValid());
			$this -> assertFalse($objComponent -> isValid());
			$this -> assertFalse($objModule -> isValid());
			$this -> assertFalse($objConfiguration -> isValid());


			// test invalid route add
			try {

				$objComponent -> addRoute($objRoute);
				$this -> fail();
			}
			catch (InvalidConfigurationException $e) {

				$this -> pass();
			}

			// test invalid component add
			try {

				$objModule -> addComponent($objComponent);
				$this -> fail();
			}
			catch (InvalidConfigurationException $e) {

				$this -> pass();
			}

			// test invalid module add
			try {

				$objConfiguration -> addModule($objModule);
				$this -> fail();
			}
			catch (InvalidConfigurationException $e) {

				$this -> pass();
			}
		}
	}



